from rest_framework.exceptions import ValidationError
from datetime import datetime
from apps.booking.validations.validate_appointment import validate_time_in_booking, validate_client_in_booking, \
    validate_provider_in_simple_appointment, validate_client_in_simple_appointment, validate_client_in_booking_repeat, \
    validate_provider_in_repeating_appointment


def validate_appointment_update(instance,request, attrs):
    start_time = instance.start_time
    end_time = instance.end_time

    if 'start_time' in attrs:
        start_time = attrs.get('start_time')
        start_time = datetime.strptime(start_time, '%H:%M:%S').time()
    if 'end_time' in attrs:
        end_time = attrs.get('end_time')
        end_time = datetime.strptime(end_time, '%H:%M:%S').time()

    clients = None
    weekly_repeat = False
    start_date = attrs.get('start_date', instance.start_date)
    if 'start_date' in attrs:
        start_date = datetime.fromtimestamp(start_date)

    select_dates = False

    if 'clients' in attrs:
        clients = attrs['clients']

    if 'weekly_repeat' in attrs:
        weekly_repeat = attrs['weekly_repeat']

    if 'select_dates' in attrs:
        select_dates = attrs['select_dates']

    if start_time and end_time:
        if start_time > end_time:
            raise ValidationError(
                "Start time must be lower than end time.")

        booking = instance.booking
        # validation if appointment has a booking
        if booking:
            validate_time_in_booking(booking, start_time, end_time)
            if clients:
                output_clients = []
                instance_uuids = set(
                    client.id for client in instance.clients.all()) if instance else set()
                for client in clients:
                    if client not in instance_uuids:
                        output_clients.append(client)
                validate_client_in_booking(
                    booking, output_clients, start_time, end_time, instance)
        if 'start_time' in attrs or 'end_time' in attrs:
            validate_provider_in_simple_appointment(request.user, start_time, end_time, start_date.date())

        elif weekly_repeat:
            if 'number_of_weeks' not in attrs:
                raise ValidationError({'msg': 'number of weeks must be provided'})
            if clients:
                output_clients = []
                instance_uuids = set(
                    client.id for client in instance.clients.all()) if instance else set()
                for client in clients:
                    if client.id not in instance_uuids:
                        output_clients.append(client)
                validate_client_in_simple_appointment(output_clients, start_time, end_time, start_date)
            validate_provider_in_simple_appointment(instance.provider, start_time, end_time,
                                                    start_date.date())

        elif select_dates:
            repeated_dates = attrs['selected_dates']
            if clients:
                output_clients = []
                instance_uuids = set(
                    client.id for client in instance.clients.all()) if instance else set()
                for client in clients:
                    if client not in instance_uuids:
                        output_clients.append(client)
                validate_client_in_booking_repeat(output_clients, start_time, end_time, repeated_dates)
            validate_provider_in_repeating_appointment(instance.provider, start_time, end_time,
                                                       repeated_dates)

        else:
            if clients:

                output_clients = []
                instance_uuids = set(
                    client.id for client in instance.clients.all()) if instance else set()
                for client in clients:
                    if client not in instance_uuids:
                        output_clients.append(client)
                validate_client_in_simple_appointment(output_clients, start_time, end_time, start_date, instance)
            # validate_provider_in_simple_appointment(self.instance.provider, start_time, end_time, start_date)

    return attrs
